home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / RGNAV.ZIP / RGNAVDB.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-25  |  6.1 KB  |  249 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Intelligent DB Navigator                        }
  4. {                                                       }
  5. {       Copyright (c) 1995,1996 by Rohit Gupta          }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit RgNavDB;
  10.  
  11. {$R-,S-,I-,O-,F-,A+,U+,K+,W-,V+,B-,X+,T-,P+,L+,Y+,D-}
  12.  
  13. interface
  14.  
  15. uses Messages, Classes, DB, DBTables, RgUseful, RgNav;
  16.  
  17. type
  18.   TNavDataLink = class;
  19.  
  20. { TRGNavigator }
  21.  
  22.   TRGNavigator = class (TRGNavigatorX)
  23.   private
  24.     FDataLink: TNavDataLink;
  25.     function  GetDataSource: TDataSource;
  26.     procedure SetDataSource(Value: TDataSource);
  27.     procedure GetKeyNames;
  28.     procedure SetKeyCaption (Initialise : Boolean);
  29.     procedure WMSize(var Message: TWMSize);  message WM_SIZE;
  30.   protected
  31.     IndexList : TStringList;
  32.     KeyNumber : SmallInt;
  33.     procedure Loaded; override;
  34.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  35.   public
  36.     constructor Create(AOwner: TComponent); override;
  37.     destructor Destroy; override;
  38.     procedure BtnClick(Index: TAllNavBtn; CallUserClick : Boolean);  override;
  39.   published
  40.     property DataSource: TDataSource read GetDataSource write SetDataSource;
  41.   end;
  42.  
  43. { TNavDataLink }
  44.  
  45.   TNavDataLink = class(TDataLink)
  46.   private
  47.     FNavigator: TRGNavigator;
  48.   protected
  49.     procedure EditingChanged; override;
  50.     procedure DataSetChanged; override;
  51.     procedure ActiveChanged; override;
  52.   public
  53.     constructor Create(ANav: TRGNavigator);
  54.     destructor Destroy; override;
  55.     procedure UpdateNavigator;
  56.   end;
  57.  
  58. procedure RegisterRGNavigator;
  59.  
  60. implementation
  61.  
  62. { TRGNavigator }
  63.  
  64. constructor TRGNavigator.Create(AOwner: TComponent);
  65. begin
  66.   inherited Create(AOwner);
  67.   IndexList := TStringList.Create;
  68.   FDataLink := TNavDataLink.Create(Self);
  69. end;
  70.  
  71. destructor TRGNavigator.Destroy;
  72. begin
  73.   FDataLink.Free;
  74.   FDataLink := nil;
  75.   IndexList.Free;
  76.   IndexList := nil;
  77.   inherited Destroy;
  78. end;
  79.  
  80. procedure TRGNavigator.Notification(AComponent: TComponent;
  81.   Operation: TOperation);
  82. begin
  83.   inherited Notification(AComponent, Operation);
  84.   if (Operation = opRemove) and (FDataLink <> nil) and
  85.     (AComponent = DataSource)
  86.   then DataSource := nil;
  87. end;
  88.  
  89. procedure TRGNavigator.WMSize(var Message: TWMSize);
  90. begin
  91.   DLEditing := FDataLink.Editing;
  92.   inherited;
  93. end;
  94.  
  95. procedure TRGNavigator.BtnClick(Index: TAllNavBtn; CallUserClick : Boolean);
  96. begin
  97.   if (DataSource <> nil) and (DataSource.State <> dsInactive) then
  98.   begin
  99.     with DataSource.DataSet do
  100.     begin
  101.       case Index of
  102.         nbPrior  : Prior;
  103.         nbNext   : Next;
  104.         nbFirst  : First;
  105.         nbLast   : Last;
  106.         nbRefresh: Refresh;
  107.         nbKey    : begin
  108.                      if KeyNumber < IndexList.Count-1
  109.                      then inc (KeyNumber)
  110.                      else KeyNumber := 0;
  111.                      SetKeyCaption (False);
  112.                    end;
  113.       else begin
  114.                 Inherited BtnClick (Index,FALSE);
  115.                 if Confirmed
  116.                 then case Index of
  117.                      nbInsert : Insert;
  118.                      nbDelete : Delete;
  119.                      nbEdit   : Edit;
  120.                      nbCancel : Cancel;
  121.                      nbPost   : Post;
  122.                      end;
  123.                 end;
  124.       end;
  125.     end;
  126.   end;
  127.   if (not (csDesigning in ComponentState))
  128.   and Assigned(FOnNavClick)
  129.   then FOnNavClick(Self, Index);
  130. end;
  131.  
  132. procedure TRGNavigator.SetDataSource(Value: TDataSource);
  133. begin
  134.   FDataLink.DataSource := Value;
  135.   if not (csLoading in ComponentState)
  136.   then begin
  137.        GetKeyNames;
  138.        ActiveChanged;
  139.   end;
  140. end;
  141.  
  142. function TRGNavigator.GetDataSource: TDataSource;
  143. begin
  144.   Result := FDataLink.DataSource;
  145. end;
  146.  
  147. procedure TRGNavigator.GetKeyNames;
  148. begin
  149.   if (assigned (FDataLink))  { Get Index Names }
  150.   then with FDataLink
  151.        do if DataSet is TTable
  152.           then with DataSet as TTable
  153.                do begin
  154.                   IndexList.Clear;
  155.                   GetIndexNames (IndexList);
  156.                   SetKeyCaption (True);
  157.                end;
  158. end;
  159.  
  160. procedure TRGNavigator.SetKeyCaption (Initialise : Boolean);
  161. begin
  162.   with IndexList
  163.   do begin
  164.      if Initialise
  165.      then begin
  166.           KeyNumber := Count-1;
  167.           while KeyNumber > 0
  168.           do begin
  169.              if TTable(FDataLink.DataSet).IndexName = Strings[KeyNumber]
  170.              then exit;
  171.              dec (KeyNumber);
  172.           end;
  173.      end;
  174.      if (Count > 0) and (KeyNumber < Count)
  175.      then begin
  176.           Buttons [nbKey].Caption := WordCase (IndexList.Strings[KeyNumber]);
  177.           TTable(FDataLink.DataSet).IndexName := Strings[KeyNumber];
  178.      end;
  179.   end;
  180. end;
  181.  
  182. procedure TRGNavigator.Loaded;
  183. begin
  184.   GetKeyNames;
  185.   inherited Loaded;
  186. end;
  187.  
  188. { TNavDataLink }
  189.  
  190. constructor TNavDataLink.Create(ANav: TRGNavigator);
  191. begin
  192.   inherited Create;
  193.   FNavigator := ANav;
  194. end;
  195.  
  196. destructor TNavDataLink.Destroy;
  197. begin
  198.   FNavigator := nil;
  199.   inherited Destroy;
  200. end;
  201.  
  202. procedure TNavDataLink.UpdateNavigator;
  203. begin
  204.   if FNavigator <> nil
  205.   then with FNavigator
  206.        do begin
  207.           DLActive  := Active;
  208.           DLEditing := Editing;
  209.           if DataSet <> nil
  210.           then DLCanModify := DataSet.CanModify;
  211.        end;
  212. end;
  213.  
  214. procedure TNavDataLink.EditingChanged;
  215. begin
  216.   UpdateNavigator;
  217.   if FNavigator <> nil
  218.   then FNavigator.EditingChanged;
  219. end;
  220.  
  221. procedure TNavDataLink.DataSetChanged;
  222. begin
  223.   UpdateNavigator;
  224.   if FNavigator <> nil
  225.   then with FNavigator, DataSet
  226.        do begin
  227.           DLBOF := BOF;
  228.           DLEOF := EOF;
  229.        end;
  230.   if FNavigator <> nil
  231.   then FNavigator.DataChanged;
  232. end;
  233.  
  234. procedure TNavDataLink.ActiveChanged;
  235. begin
  236.   UpdateNavigator;
  237.   if FNavigator <> nil
  238.   then FNavigator.ActiveChanged;
  239. end;
  240.  
  241. { Register }
  242.  
  243. procedure RegisterRGNavigator;
  244. begin
  245.   RegisterComponents('RG', [TRGNavigator]);
  246. end;
  247.  
  248. end.
  249.